# -*- coding: utf-8 -*-
import scrapy


class TestspiderSpider(scrapy.Spider):
    name = 'TestSpider'
    allowed_domains = ['lab.scrapyd.cn']
    start_urls = ['http://lab.scrapyd.cn/']

    def parse(self, response):
        mingyan = response.xpath("//div[@class='quote post']")
        for v in mingyan:
            text = v.xpath(".//span[@class='text']//text()").extract_first()

            author = v.xpath(".//small[@class='author']//text()").extract_first()

            tags = v.xpath(".//div[@class='tags']//a//text()").extract()
            tags = ",".join(tags)
            fileName = "C:\\Users\\shengong\\Desktop\\%s-语录.txt" %author
            with open(fileName,"a+") as f:
                f.write(text)
                f.write('\n')
                f.write('标签：'+tags)
                f.write('\n------------\n')
                f.close()
        next_link = response.xpath("//li[@class='next']/a/@href").extract()
        if next_link:
            next_link = next_link[0]
            next_link = response.urljoin(next_link)
            yield scrapy.Request(next_link,callback=self.parse)